package com.gopay.bis.kuaiqian.ebank.impl;

import javax.annotation.Resource;

import org.apache.axis.utils.StringUtils;
import org.springframework.stereotype.Service;

import com.gopay.bis.kuaiqian.ebank.operation.KuaiQianQueryHelper;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.kuaiqian.ebank.GatewayOrderQueryResponse;
import com.gopay.common.domain.bank.kuaiqian.ebank.KuaiQianMessage;
import com.gopay.common.domain.bank.kuaiqian.ebank.KuaiQianQueryMessage;
import com.gopay.common.domain.bank.kuaiqian.ebank.KuaiQianQueryResultMessage;
import com.gopay.common.domain.bank.kuaiqian.ebank.KuaiQianResultMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;
import com.gopay.remote.bank.businesses.BankService;

@Service("kuaiQianBankService")
public class KuaiQianBankServiceImpl implements BankService {

	@Resource(name = "kuaiQianCertHelper")
	private ICertHelper kuaiQianCertHelper;
	
	@Resource(name="kuaiQianQueryHelper")
	private KuaiQianQueryHelper kuaiQianQueryHelper;

	
	public Object query(Object message) throws GopayException {
		 KuaiQianQueryMessage queryMessage = (KuaiQianQueryMessage)message;
	        String signStr = (String)kuaiQianQueryHelper.signByMD5(queryMessage.getPlainStr());
	        queryMessage.setSignMsg(signStr);
	        GatewayOrderQueryResponse response = kuaiQianQueryHelper.query(queryMessage);
	        if(response !=null ){
	            if(StringUtils.isEmpty(response.getErrCode())){
	                return kuaiQianQueryHelper.parse(response);
	            }else{
	                KuaiQianQueryResultMessage  resMsg = new KuaiQianQueryResultMessage();
	                resMsg.setErrCode(response.getErrCode());
	                resMsg.setMerchantAcctId(response.getMerchantAcctId());
	                return resMsg;
	            }
	        }else{
	            return null;
	        }
	        
	}

	
	public Object sign(BaseMessage message) throws GopayException {
		KuaiQianMessage kqMessage =  (KuaiQianMessage)message;
		return  (String) kuaiQianCertHelper.sign(kqMessage);
	}

	
	public Object verify(BaseMessage baseMessage) throws GopayException {
		KuaiQianResultMessage message = (KuaiQianResultMessage) baseMessage;
		return (Boolean) kuaiQianCertHelper.verify(message);
	}


	@Override
	public Object dayend(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Object refund(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}


}
